<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      Array.prototype.myReduce = function (fun, init) {
        // 判断是否传入初始值
        let accumulator = init !== undefined ? init : this[0];
        // 循环遍历数组，更新累加器的值
        for (let i = init !== undefined ? 0 : 1; i < this.length; i++) {
          accumulator = fun(accumulator, this[i], i, this);
        }
        // 返回累加器的最终值
        return accumulator;
      };
      let arr = [1, 2, 3, 5, 8];
      let total = arr.myReduce((sum, item) => {
        return (sum += item);
      }, 0);
      console.log(total);
    </script>
  </body>
</html>
